
<!doctype html>
<head>
  <meta charset="utf-8"/>
</head>
<body>
<div>可打开浏览器控制台查看结果</div>

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/crypto-js/4.0.0/crypto-js.js"></script>
<script type="text/javascript">
  var appKey = '34c888ea89330fad';
  var key = 'JNMqwdnOICD7HzCQzktypYkQYBP0AITE';//注意：暴露appSecret，有被盗用造成损失的风险
  var salt = (new Date).getTime();
  var curtime = Math.round(new Date().getTime()/1000);
  var query = '您好，欢迎再次使用有道智云文本翻译API接口服务';
  // 多个query可以用\n连接  如 query='apple\norange\nbanana\npear'
  var from = 'zh-CHS';
  var to = 'en';
  var str1 = appKey + truncate(query) + salt + curtime + key;
  var vocabId =  '您的用户词表ID';
  //console.log('---',str1);

  var sign = CryptoJS.SHA256(str1).toString(CryptoJS.enc.Hex);
  $.ajax({
    url: 'http://openapi.youdao.com/api',
    type: 'post',
    dataType: 'jsonp',
    data: {
      q: query,
      appKey: appKey,
      salt: salt,
      from: from,
      to: to,
      sign: sign,
      signType: "v3",
      curtime: curtime,
      vocabId: vocabId,
    },
    success: function (data) {
      console.log(data);
    }
  });

  function truncate(q){
    var len = q.length;
    if(len<=20) return q;
    return q.substring(0, 10) + len + q.substring(len-10, len);
  }
</script>
</body>
